package com.android.lovely.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.android.lovely.api.service.userService
import com.android.lovely.data.CreatorRole
import com.android.lovely.data.RoleHistory
import com.android.lovely.framework.BaseLoadViewModel
import com.android.lovely.framework.BaseViewModel
import com.android.lovely.room.role.CreateRole
import com.android.lovely.room.role.CreateRoleDataBase
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow

/**
 *
 * @author why
 * @date 2025/4/8 16:41
 */
class WorkCreateViewModel: BaseViewModel() {

    private val _roleType = MutableLiveData<Pair<Int, Int>>()
    val roleType: LiveData<Pair<Int, Int>> get() = _roleType

    private val _updateList = MutableLiveData<Pair<Int,List<RoleHistory>>>()
    val updateList: LiveData<Pair<Int,List<RoleHistory>>> get() = _updateList

    private val _updateLocalList = MutableLiveData<List<CreateRole>>()
    val updateLocalList: LiveData<List<CreateRole>> get() = _updateLocalList

    var taskType = 0

    var position = 0

    var currentPages = 1

    fun updateRole(trans: Int, type: Int){
        _roleType.value = Pair(trans, type)
    }

    fun requestRole(){
        viewModelScope.launchSafety{
            currentPages = 1
            if((_roleType.value?.first ?: 0) == 2) {
                val data = CreateRoleDataBase.dao.getRoles()
                _updateLocalList.postValue(data)
            } else {
                val status = when(_roleType.value?.second ?: 0) {
                    1 -> 1
                    2 -> 3
                    3 -> 2
                    else -> null
                }
                val data = userService.getMyRoles(
                    mapOf(
                        "page" to currentPages,
                        "size" to 10,
                        "status" to status,
                        "privacy" to  (_roleType.value?.first ?: 0) +1
                    )
                ).getOrDefault("role_list", emptyList())
                val list = arrayListOf<RoleHistory>()
                list.addAll(data)
//                if(data.size >= 10) {
//                    list.add(RoleHistory(
//                        id = -1
//                    ))
//                }
//                _updateList.value?.toMutableList()?.addAll(list)
                _updateList.postValue(Pair(currentPages, list))
                currentPages++
            }
        }
    }

    fun loadMore(onNext: () -> Unit) {
        viewModelScope.launchSafety{
            if((_roleType.value?.first ?: 0) == 2) {
                val data = CreateRoleDataBase.dao.getRoles()
                _updateLocalList.value = data
                onNext.invoke()
            } else {
                val status = when(_roleType.value?.second ?: 0) {
                    1 -> 1
                    2 -> 3
                    3 -> 2
                    else -> null
                }
                val data = userService.getMyRoles(
                    mapOf(
                        "page" to currentPages,
                        "size" to 10,
                        "status" to status,
                        "privacy" to  (_roleType.value?.first ?: 0) +1
                    )
                ).getOrDefault("role_list", emptyList())
                val list = arrayListOf<RoleHistory>()
                list.addAll(data)
//                if(data.size < 10) {
//                    list.add(RoleHistory(
//                        id = -1
//                    ))
//                }
                _updateList.postValue(Pair(currentPages, list))
                onNext.invoke()
                currentPages++
            }
        }
    }

}